import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _typeof from "@babel/runtime/helpers/esm/typeof";
var _excluded = ["label", "prefixCls", "onChange", "value", "mode", "children", "defaultValue", "size", "showSearch", "disabled", "style", "className", "bordered", "options", "onSearch", "allowClear", "labelInValue", "fieldNames", "lightLabel", "labelTrigger"];
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { SearchOutlined } from '@ant-design/icons';
import { FieldLabel } from '@ant-design/pro-utils';
import { ConfigProvider, Input, Select } from 'antd-v4';
import classNames from 'classnames';
import React, { useContext, useMemo, useState } from 'react';
import './index.less';
/**
 * 如果有 label 就优先使用 label
 *
 * @param valueMap
 * @param v
 */

var getValueOrLabel = function getValueOrLabel(valueMap, v) {
  if (_typeof(v) !== 'object') {
    return valueMap[v] || v;
  }

  return valueMap[v === null || v === void 0 ? void 0 : v.value] || v.label;
};

var LightSelect = function LightSelect(props, ref) {
  var label = props.label,
      customizePrefixCls = props.prefixCls,
      _onChange = props.onChange,
      value = props.value,
      mode = props.mode,
      children = props.children,
      defaultValue = props.defaultValue,
      size = props.size,
      showSearch = props.showSearch,
      disabled = props.disabled,
      style = props.style,
      className = props.className,
      bordered = props.bordered,
      options = props.options,
      onSearch = props.onSearch,
      allowClear = props.allowClear,
      labelInValue = props.labelInValue,
      fieldNames = props.fieldNames,
      lightLabel = props.lightLabel,
      labelTrigger = props.labelTrigger,
      restProps = _objectWithoutProperties(props, _excluded);

  var _props$placeholder = props.placeholder,
      placeholder = _props$placeholder === void 0 ? label : _props$placeholder;

  var _ref = fieldNames || {},
      _ref$label = _ref.label,
      labelPropsName = _ref$label === void 0 ? 'label' : _ref$label,
      _ref$value = _ref.value,
      valuePropsName = _ref$value === void 0 ? 'value' : _ref$value;

  var _useContext = useContext(ConfigProvider.ConfigContext),
      getPrefixCls = _useContext.getPrefixCls;

  var prefixCls = getPrefixCls('pro-field-select-light-select');

  var _useState = useState(false),
      _useState2 = _slicedToArray(_useState, 2),
      open = _useState2[0],
      setOpen = _useState2[1];

  var _useState3 = useState(''),
      _useState4 = _slicedToArray(_useState3, 2),
      keyword = _useState4[0],
      setKeyword = _useState4[1];

  var valueMap = useMemo(function () {
    var values = {};
    options === null || options === void 0 ? void 0 : options.forEach(function (item) {
      var optionLabel = item[labelPropsName];
      var optionValue = item[valuePropsName];
      values[optionValue] = optionLabel || optionValue;
    });
    return values;
  }, [labelPropsName, options, valuePropsName]);
  var filterValue = Array.isArray(value) ? value.map(function (v) {
    return getValueOrLabel(valueMap, v);
  }) : getValueOrLabel(valueMap, value);
  return _jsxs("div", {
    className: classNames(prefixCls, _defineProperty({}, "".concat(prefixCls, "-searchable"), showSearch), className),
    style: style,
    onClick: function onClick(e) {
      var _lightLabel$current, _lightLabel$current$l, _lightLabel$current$l2;

      if (disabled) return; // 点击label切换下拉菜单

      var isLabelClick = lightLabel === null || lightLabel === void 0 ? void 0 : (_lightLabel$current = lightLabel.current) === null || _lightLabel$current === void 0 ? void 0 : (_lightLabel$current$l = _lightLabel$current.labelRef) === null || _lightLabel$current$l === void 0 ? void 0 : (_lightLabel$current$l2 = _lightLabel$current$l.current) === null || _lightLabel$current$l2 === void 0 ? void 0 : _lightLabel$current$l2.contains(e.target);

      if (isLabelClick) {
        setOpen(!open);
      } else {
        setOpen(true);
      }
    },
    children: [_jsx(Select, _objectSpread(_objectSpread({}, restProps), {}, {
      allowClear: allowClear,
      value: value,
      mode: mode,
      labelInValue: labelInValue,
      size: size,
      disabled: disabled,
      onChange: function onChange(v, option) {
        _onChange === null || _onChange === void 0 ? void 0 : _onChange(v, option);

        if (mode !== 'multiple') {
          setTimeout(function () {
            setOpen(false);
          }, 0);
        }
      },
      bordered: bordered,
      showSearch: showSearch,
      onSearch: onSearch,
      style: style,
      dropdownRender: function dropdownRender(menuNode) {
        return _jsxs("div", {
          ref: ref,
          children: [showSearch && _jsx("div", {
            style: {
              margin: '4px 8px'
            },
            children: _jsx(Input, {
              value: keyword,
              allowClear: allowClear,
              onChange: function onChange(e) {
                setKeyword(e.target.value.toLowerCase());
                onSearch === null || onSearch === void 0 ? void 0 : onSearch(e.target.value);
              },
              onKeyDown: function onKeyDown(e) {
                // 避免按下删除键把选项也删除了
                e.stopPropagation();
              },
              style: {
                width: '100%'
              },
              prefix: _jsx(SearchOutlined, {})
            })
          }), menuNode]
        });
      },
      open: open,
      onDropdownVisibleChange: function onDropdownVisibleChange(isOpen) {
        if (!isOpen) {
          setTimeout(function () {
            setKeyword('');
          }, 0);
        }

        if (!labelTrigger) {
          setOpen(isOpen);
        }
      },
      prefixCls: customizePrefixCls,
      options: keyword ? options === null || options === void 0 ? void 0 : options.filter(function (o) {
        var _String, _String$toLowerCase, _o$valuePropsName, _o$valuePropsName$toS, _o$valuePropsName$toS2;

        return ((_String = String(o[labelPropsName])) === null || _String === void 0 ? void 0 : (_String$toLowerCase = _String.toLowerCase()) === null || _String$toLowerCase === void 0 ? void 0 : _String$toLowerCase.includes(keyword)) || ((_o$valuePropsName = o[valuePropsName]) === null || _o$valuePropsName === void 0 ? void 0 : (_o$valuePropsName$toS = _o$valuePropsName.toString()) === null || _o$valuePropsName$toS === void 0 ? void 0 : (_o$valuePropsName$toS2 = _o$valuePropsName$toS.toLowerCase()) === null || _o$valuePropsName$toS2 === void 0 ? void 0 : _o$valuePropsName$toS2.includes(keyword));
      }) : options
    })), _jsx(FieldLabel, {
      ellipsis: true,
      size: size,
      label: label,
      placeholder: placeholder,
      disabled: disabled,
      expanded: open,
      bordered: bordered,
      allowClear: allowClear,
      value: filterValue || (value === null || value === void 0 ? void 0 : value.label) || value,
      onClear: function onClear() {
        _onChange === null || _onChange === void 0 ? void 0 : _onChange(undefined, undefined);
      },
      ref: lightLabel
    })]
  });
};

export default /*#__PURE__*/React.forwardRef(LightSelect);